package com.b.controller;

import com.b.domain.SeckillActivity;
import com.b.service.OrderService;
import com.b.service.SeckillActivityService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author jdj
 * @Date 2025/7/25
 */
@RestController
@RequestMapping("/seckill")
public class SeckillController {
    @Resource
    private OrderService orderService;
    @Resource
    private SeckillActivityService seckillActivityService;

    // 秒杀订单创建
    @PostMapping("create")
    public Boolean create(@RequestParam("userId") Integer userId,
                          @RequestParam("productId") Integer productId,
                          @RequestParam("quantity") Integer quantity) {
        return orderService.createSeckillOrder(userId, productId, quantity);
    }

    /**
     * @param page
     * @param pageSize
     * @param name
     * @return org.springframework.data.domain.Page<com.b.domain.SeckillActivity>
     * @description: 秒杀活动列表
     * @author jdj
     * @date 2025/7/26
     */

    @GetMapping("list")
    public Page<SeckillActivity> list(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", required = false) String name
    ) {
//        System.out.println("page = " + page + ", pageSize = " + pageSize + ", name = " + name);
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        return seckillActivityService.list(name, pageRequest);
    }

    //修改或者添加活动
    @PostMapping("save")
    public Boolean save( @RequestBody SeckillActivity seckillActivity) {
        if (seckillActivity.getId() == null) {
            // 添加
            return seckillActivityService.insertSelective(seckillActivity);
        } else {
            // 修改
            return seckillActivityService.updateByPrimaryKeySelective(seckillActivity);
        }
    }

    //修改活动状态
    @PostMapping("/updateStatus")
    public Boolean updateStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        return seckillActivityService.updateStatus(id, status);

    }
    //删除活动
    @PostMapping("/delete/{id}")
    public Boolean delete(@PathVariable("id") Integer id) {
        return seckillActivityService.deleteByPrimaryKey(id);
    }

}
